package com.zhuiyun.project.api.groupmessage.mapper;

import com.zhuiyun.project.api.groupmessage.entity.GroupMessage;
import com.zhuiyun.project.api.groupmessage.model.GroupMessageModel;
import com.zhuiyun.project.api.personmessage.entity.PersonMessage;
import com.zhuiyun.project.common.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

/**
 * @ClassName GroupMessageMapper
 * @Description TODO
 * @Author kdj
 * @Date 2023/7/24 19:51
 **/
@Mapper
public interface GroupMessageMapper extends BaseMapper<GroupMessage> {
    /**
     * @param groupId
     * @Author kdj
     * @Description 根据群聊id 获取群聊列表消息
     * @Date 2023/7/25 10:43
     * @Return java.util.List
     */
    @Select("SELECT" +
            "    gm.*," +
            "    message.uuid," +
            "    user_data.fromName," +
            "    user_data.fromImgId" +
            " FROM" +
            "    group_message gm " +
            " INNER JOIN" +
            "    message ON gm.message_id = message.id" +
            " LEFT JOIN (" +
            "    SELECT" +
            "        id," +
            "        user_name AS fromName," +
            "        image_id AS fromImgId," +
            "        'app_user' AS user_type" +
            "    FROM" +
            "        app_user" +
            "    UNION ALL" +
            "    SELECT" +
            "        id," +
            "        user_name AS fromName," +
            "        avatar_id AS fromImgId," +
            "        'sys_user' AS user_type" +
            "    FROM" +
            "        sys_user" +
            " ) AS user_data ON gm.from_id = user_data.id AND" +
            "                 (" +
            "                     (gm.user_type = 1 AND user_data.user_type = 'app_user') OR" +
            "                     (gm.user_type = 0 AND user_data.user_type = 'sys_user')" +
            "                 )" +
            " WHERE" +
            "    gm.to_id = #{groupId} AND" +
            "    gm.del_flag = 0 " +
            " ORDER BY" +
            "    gm.create_time ASC ")
    List<GroupMessageModel> getGroupChatListById(int groupId);

    /**
     * @param groupId
     * @Author kdj
     * @Description 根据群聊id 获取群聊列表消息
     * @Date 2023/7/25 10:43
     * @Return java.util.List
     */
    @Select("SELECT" +
            "    gm.*," +
            "    message.uuid," +
            "    user_data.fromName," +
            "    user_data.fromImgId" +
            " FROM" +
            "    group_message gm " +
            " INNER JOIN" +
            "    message ON gm.message_id = message.id" +
            " LEFT JOIN (" +
            "    SELECT" +
            "        id," +
            "        user_name AS fromName," +
            "        image_id AS fromImgId," +
            "        'app_user' AS user_type" +
            "    FROM" +
            "        app_user" +
            "    UNION ALL" +
            "    SELECT" +
            "        id," +
            "        user_name AS fromName," +
            "        avatar_id AS fromImgId," +
            "        'sys_user' AS user_type" +
            "    FROM" +
            "        sys_user" +
            " ) AS user_data ON gm.from_id = user_data.id AND" +
            "                 (" +
            "                     (gm.user_type = 1 AND user_data.user_type = 'app_user') OR" +
            "                     (gm.user_type = 0 AND user_data.user_type = 'sys_user')" +
            "                 )" +
            " WHERE" +
            "    gm.to_id = #{groupId} AND" +
            "    gm.del_flag = 0 AND" +
            "    gm.update_time < #{date}" +
            " ORDER BY" +
            "    gm.create_time DESC ")
    List<GroupMessageModel> getGroupChatListByIdDesc(int groupId, Date date);

    /**
     * @param groupId
     * @Author kdj
     * @Description 获取群聊最后一条消息 针对所有人
     * @Date 2023/7/26 16:26
     * @Return com.zhuiyun.project.api.groupmessage.model.GroupMessageModel
     */
    @Select("select group_message.*,message.uuid from group_message left join message on group_message.message_id=message.id " +
            " where group_message.to_id=#{groupId} and del_flag=0  order by create_time desc limit 1")
    GroupMessageModel getGroupChatLast(int groupId);

    /**
     * @param deleteUserId
     * @param id
     * @Author kdj
     * @Description 删除消息
     * @Date 2023/7/20 15:13
     * @Return int
     */
    @Update("UPDATE group_message SET delete_user_id = #{deleteUserId} WHERE id = #{id};")
    int upDelMsg(String deleteUserId, Integer id);

    /**
     * @param fromId
     * @param open
     * @param size
     * @Author kdj
     * @Description 获取群聊最后一条消息 针对自己
     * @Date 2023/7/31 12:09
     * @Return java.util.List<com.zhuiyun.project.api.personmessage.model.PersonMessageModel>
     */
    @Select("select group_message.*,message.uuid from group_message left join message on group_message.message_id=message.id " +
            " where group_message.to_id=#{fromId} and del_flag=0 order by create_time desc limit #{open},#{size}")
    List<GroupMessageModel> getChatRecordLimit(int fromId, int open, int size);

    /**
     * @param uuid
     * @Author kdj
     * @Description 根据uuid找到groupMessage
     * @Date 2023/7/31 15:30
     * @Return com.zhuiyun.project.api.groupmessage.entity.GroupMessage
     */


    /**
     * @param msgByUuid
     * @Author kdj
     * @Description 判断这个消息是不是最后一条消息
     * @Date 2023/7/31 16:52
     * @Return java.lang.Boolean
     */
    @Select("select id from group_message WHERE to_id=#{toId} and del_flag = 0  order by create_time desc limit 1")
    int isLast(GroupMessage msgByUuid);



    @Select("select group_message.*,message.uuid from message,group_message WHERE group_message.message_id=message.id and  message.uuid=#{uuid} and del_flag = 0")
    GroupMessage getMsgByUuid(String uuid);
}
